#include <iostream>
#include "CallableThread.h"
#include <unistd.h>
#include <thread>
#include <chrono>
#include <functional>
#include <future>

using std::cout;
using std::endl;
using std::function;

namespace callable {
    namespace thread {

        void CallableThread::forkProc(function<void()> callable) {
            pid_t pid = fork();
            if (pid != 0) {
                cout << "callable father process is return" << endl;
                return;
            }
            for (const auto &func: execMethods){
                func();
                std::this_thread::sleep_for(std::chrono::milliseconds(100));
            }
            if (callable!= nullptr){
               callable();
            }
            exit(0);
        }

        void CallableThread::execMethodAll(function<void()> callable) {
            cout << "callable execMethodAll" << endl;
            fillVec();
            forkProc(callable);
            std::this_thread::sleep_for(std::chrono::milliseconds(150));
            cout << "callable father process is finish" << endl;


        }
        void CallableThread::fillVec(){
            CallableThread obj = *this;
//            execMethods.emplace_back(std::bind(&CallableThread::methodA, &obj));
//            execMethods.emplace_back(std::bind(&CallableThread::methodB, &obj));
//            execMethods.emplace_back(std::bind(&CallableThread::methodC, &obj));
            execMethods.emplace_back(methodA);
            execMethods.emplace_back(methodB);
            execMethods.emplace_back(methodC);
        }

        void CallableThread::methodA() {
            cout << "methodA execute" << endl;
        }
        void CallableThread::methodB() {
            cout << "methodB execute" << endl;
        }
        void CallableThread::methodC() {
            cout << "methodC execute" << endl;
        }

    } // thread
} // callable